package com.hope.hotel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hope.hotel.common.Result;
import com.hope.hotel.entity.po.Message;
import com.hope.hotel.entity.vo.MessageVO;
import com.hope.hotel.mapper.MessageMapper;
import com.hope.hotel.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息通知服务实现类
 */
@Service
@RequiredArgsConstructor
public class MessageServiceImpl implements MessageService {
    
    private final MessageMapper messageMapper;
    
    @Override
    public Result getUserMessages(String userId, Integer page, Integer pageSize) {
        // 分页查询
        Page<Message> messagePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getUserId, userId)
                .orderByDesc(Message::getCreateTime);
        
        Page<Message> resultPage = messageMapper.selectPage(messagePage, queryWrapper);
        
        // 转换为VO
        List<MessageVO> messageVOList = resultPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
        
        return Result.success(messageVOList, String.valueOf(resultPage.getTotal()));
    }
    
    @Override
    public Result markAsRead(Integer messageId, String userId) {
        // 查询消息是否存在
        Message message = messageMapper.selectById(messageId);
        if (message == null) {
            return Result.error("消息不存在");
        }
        
        // 验证消息所属用户
        if (!message.getUserId().equals(userId)) {
            return Result.error("无权操作此消息");
        }
        
        // 标记为已读
        message.setIsRead(true);
        messageMapper.updateById(message);
        
        return Result.success("标记成功");
    }
    
    @Override
    public Result deleteMessage(Integer messageId, String userId) {
        // 查询消息是否存在
        Message message = messageMapper.selectById(messageId);
        if (message == null) {
            return Result.error("消息不存在");
        }
        
        // 验证消息所属用户
        if (!message.getUserId().equals(userId)) {
            return Result.error("无权操作此消息");
        }
        
        // 删除消息
        messageMapper.deleteById(messageId);
        
        return Result.success("删除成功");
    }
    
    @Override
    public Result createMessage(Message message) {
        messageMapper.insert(message);
        return Result.success("创建成功");
    }
    
    @Override
    public Result<Integer> getUnreadCount(String userId) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getUserId, userId)
                .eq(Message::getIsRead, false);
        
        Integer count = Math.toIntExact(messageMapper.selectCount(queryWrapper));
        return Result.success(count);
    }
    
    /**
     * 将Message实体转换为MessageVO
     */
    private MessageVO convertToVO(Message message) {
        MessageVO messageVO = new MessageVO();
        BeanUtils.copyProperties(message, messageVO);
        
        // 手动设置read字段，因为属性名不同
        messageVO.setRead(message.getIsRead());
        
        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        messageVO.setDate(message.getCreateTime().format(formatter));
        
        return messageVO;
    }
} 